package com.aloha.springframework.aop.aspectj;

import com.aloha.springframework.aop.framework.ReflectiveMethodInvocation;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

/**
 * @author DaiZhiHeng
 * @description AspectJ的环绕通知
 * @date 2023/8/2 22:21
 */
public class AspectJAroundAdvice extends AbstractAspectJAdvice implements MethodInterceptor {

    public AspectJAroundAdvice(Method aspectJAdviceMethod, AspectInstanceFactory aspectInstanceFactory) {
        super(aspectJAdviceMethod, aspectInstanceFactory);
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        return around(getProceedingJoinPoint((ReflectiveMethodInvocation) invocation));
    }

    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        return invokeAdviceMethod(pjp);
    }

    protected ProceedingJoinPoint getProceedingJoinPoint(ReflectiveMethodInvocation mi) {
        return new MethodInvocationProceedingJoinPoint(mi);
    }
}
